package com.sskj.mine.ui.activity;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.Image;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.gamerole.orcameralib.CameraActivity;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.lib.BaseHttpConfig;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.router.provider.LogoutProvider;
import com.sskj.lib.util.CommonUtil;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.bean.PersonCenterItemBean;
import com.sskj.mine.component.DaggerUserDataComponent;
import com.sskj.mine.http.HttpConfig;
import com.sskj.mine.presenter.PersonCenterActivityPresenter;
import com.sskj.mine.router.LogoutProviderImpl;
import com.sskj.mine.util.HideUtil;
import com.sskj.mine.util.LogoutUtil;

import org.w3c.dom.Text;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

@Route(path = RConfig.MINE_PERSON_CENTER)//个人中心
public class PersonCenterActivity extends BaseActivity<PersonCenterActivityPresenter> {
    @Inject
    UserViewModel userViewModel;
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.btSubmit)
    Button btSubmit;
    private UserData userData;
    private SlimAdapter<PersonCenterItemBean> slimAdapter;
    private static final int REQUEST_A_CAMERA = 1005;
    private String mImagePath;
    ;

    @Override
    protected int getLayoutResId() {
        return R.layout.mine_activity_person_center;
    }

    @Override
    public PersonCenterActivityPresenter getPresenter() {
        return new PersonCenterActivityPresenter();
    }

    @Override
    protected void initView() {
        DaggerUserDataComponent.create().inject(this);
        setTitle("个人信息");
        initRecy();
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            }
            updateUI();
        });
        ClickUtil.click(btSubmit, () -> {
            new LogoutProviderImpl().logout();
        });
    }

    private void initRecy() {
        ArrayList<PersonCenterItemBean> itemBeans = new ArrayList<>();
        itemBeans.add(new PersonCenterItemBean("头像", "", true));
        itemBeans.add(new PersonCenterItemBean(App.INSTANCE.getString(R.string.minePersonCenterActivity1), "", false));
        itemBeans.add(new PersonCenterItemBean("邮箱", "", false));
        itemBeans.add(new PersonCenterItemBean(App.INSTANCE.getString(R.string.minePersonCenterActivity3), "", false));
        itemBeans.add(new PersonCenterItemBean(App.INSTANCE.getString(R.string.minePersonCenterActivity5), "", false));
        itemBeans.add(new PersonCenterItemBean(App.INSTANCE.getString(R.string.minePersonCenterActivity7), "", false));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setFirstDraw(false)
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.transparent))
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 2)));
        slimAdapter = SlimAdapter.create().register(R.layout.mine_recy_item_person_center, new SlimInjector<PersonCenterItemBean>() {
            @Override
            public void onInject(PersonCenterItemBean data, IViewInjector injector, List payloads) {
                if (data.isShowImg()) {
                    ImageUtil.setCircleImage(data.getImgUrl(), (ImageView) injector.findViewById(R.id.showImg));
                }
                injector
                        .text(R.id.tvTitle, data.getTitle())
                        .visibility(R.id.showImg, data.isShowImg() ? View.VISIBLE : View.GONE)
                        .clicked(R.id.showImg, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                PictureSelector.create(PersonCenterActivity.this)
                                        .openGallery(PictureMimeType.ofImage())
                                        .selectionMode(PictureConfig.SINGLE)
                                        .compress(true)
                                        .forResult(PictureConfig.CHOOSE_REQUEST);
                            }
                        })
                        .text(R.id.tvContent, data.getContent());
            }
        }).attachTo(recyclerView).updateData(itemBeans);
    }

    private void updateUI() {
        if (userData != null) {
            slimAdapter.getData().get(0).setContent("");
            slimAdapter.getData().get(0).setImgUrl(HttpConfig.BASE_IMG_URL + userData.getHeadUrl());
            slimAdapter.getData().get(1).setContent(TextUtils.isEmpty(userData.getTel()) ? "--" : HideUtil.getPhoneHide(userData.getTel()));
            slimAdapter.getData().get(2).setContent(TextUtils.isEmpty(userData.getEmail()) ? "--" : CommonUtil.dealEmail(userData.getEmail()));
            slimAdapter.getData().get(3).setContent(userData.getAccount());
            if (userData.getBasicStatus() == 2) {
                slimAdapter.getData().get(4).setContent(TextUtils.isEmpty(userData.getUsername()) ? App.INSTANCE.getString(R.string.mine_un_certification) : userData.getUsername());
                slimAdapter.getData().get(5).setContent(TextUtils.isEmpty(userData.getIdCardNo()) ? App.INSTANCE.getString(R.string.mine_un_certification) : HideUtil.getIDCardHide(userData.getIdCardNo()));
            } else {
                slimAdapter.getData().get(4).setContent("--");
                slimAdapter.getData().get(5).setContent("--");
            }
            slimAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public boolean getImmersion() {
        return false;
    }


    public void updatePath(String path) {
        slimAdapter.getData().get(0).setImgUrl(BaseHttpConfig.BASE_IMG_URL + path);
        slimAdapter.notifyItemChanged(0);
        userViewModel.update();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 图片、视频、音频选择结果回调
        if (resultCode == RESULT_OK && requestCode == PictureConfig.CHOOSE_REQUEST) {
            List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
            mImagePath = TextUtils.isEmpty(selectList.get(0).getCompressPath()) ? selectList.get(0).getPath() : selectList.get(0).getCompressPath();
            mPresenter.uploadImg(new File(mImagePath));
        }
    }


}
